﻿using AccompanyCabinet.Helper;
using AccompanyCabinet.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccompanyCabinet.Admin
{
    public partial class UpdateDeviceBox : Form
    {
        public UpdateDeviceBox()
        {
            InitializeComponent();
        }
        public DeviceBoxList devRefresh = null;
        string Statusno = null;
        string Id = null;
        public UpdateDeviceBox (string id,string num,string statusno,string lockid,string locknum)
        {
            InitializeComponent();
            txt_num.Text = num;
            txt_lockId.Text = lockid;
            Statusno = statusno;
            txt_lockNum.Text = locknum;
            Id = id;
        }

        private void UpdateDeviceBox_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("id");
            DataColumn dc2 = new DataColumn("name");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            DataRow dr1 = dt.NewRow();
            dr1["id"] = "Big";
            dr1["name"] = "大";
            DataRow dr2 = dt.NewRow();
            dr2["id"] = "Mid";
            dr2["name"] = "中";
            DataRow dr3 = dt.NewRow();
            dr3["id"] = "Smell";
            dr3["name"] = "小";
            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);
            com_statusno.DataSource = dt;
            com_statusno.ValueMember = "id";
            com_statusno.DisplayMember = "name";
            if (Statusno == "大")
            {
                com_statusno.SelectedValue = "Big";
            }
            if (Statusno == "中")
            {
                com_statusno.SelectedValue = "Mid";
            }
            if (Statusno == "小")
            {
                com_statusno.SelectedValue = "Smell";
            }
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            kb.Close();
            if (txt_lockId.Text == null || txt_lockNum.Text == null || txt_num == null)
            {
                MessageBox.Show("内容不能为空");
                return;
            }
            string url = "TerminalInfo/AddOrUpdateBox";
            
            if (Id == null)
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("num", txt_num.Text);
                parameters.Add("lockId", txt_lockId.Text);
                parameters.Add("lockNum", txt_lockNum.Text);
                parameters.Add("typeNo", com_statusno.SelectedValue.ToString());
                parameters.Add("updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                parameters.Add("biId","0");
                parameters.Add("identity", HttpHelper.deviceInfoModel.DId.ToString());
                var res = HttpHelper.Post(parameters, url, 10);
                //var res = HttpHelper.Post(url+ "num=" + txt_num.Text + "&lockId=" + txt_lockId.Text + "&lockNum=" + txt_lockNum.Text + "&typeNo=" + com_statusno.SelectedValue + "&updateTime=" +DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")+ "&biId=0&identity=" + 10 + "");
                if (res.status == "OK")
                {
                    DBoxList dBox = new DBoxList();
                    dBox.id = Convert.ToInt32(res.Message);
                    dBox.LockId = Convert.ToInt32(txt_lockId.Text);
                    dBox.Num = Convert.ToInt32(txt_num.Text);
                    dBox.StatusNo = "Unuse";
                    dBox.LockNum = Convert.ToInt32(txt_lockNum.Text);
                    dBox.TypeNo = com_statusno.SelectedValue.ToString();
                    dBox.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    HttpHelper.dBoxLists.Add(dBox);
                    MessageBox.Show("添加完成");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败");
                    this.Close();
                }                
            }
            else
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("num", txt_num.Text);
                parameters.Add("lockId", txt_lockId.Text);
                parameters.Add("lockNum", txt_lockNum.Text);
                parameters.Add("typeNo", com_statusno.SelectedValue.ToString());
                //parameters.Add("updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                parameters.Add("biId", Id);
                var res = HttpHelper.Post(parameters, url, 10);
                //var res = HttpHelper.Get(url + "?biId=" + Id + "&num=" + txt_num.Text + "&lockId=" + txt_lockId.Text + "&lockNum=" + txt_lockNum.Text + "&typeNo=" + com_statusno.SelectedValue + "");
                if (res.status == "OK")
                {
                    foreach (var item in HttpHelper.dBoxLists)
                    {
                        if (item.id.Equals(Convert.ToInt32(Id)))
                        {
                            item.Num = Convert.ToInt32(txt_num.Text);
                            item.LockId = Convert.ToInt32(txt_lockId.Text);
                            item.LockNum = Convert.ToInt32(txt_lockNum.Text);
                            item.TypeNo = com_statusno.SelectedValue.ToString();
                            item.StatusNo = "Unuse";
                            item.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff");
                        }
                    }
                    MessageBox.Show("修改完成");
                    this.Close();
                    devRefresh.Query_module();
                }
                else
                {
                    MessageBox.Show("修改失败");
                    this.Close();
                }
            }   
        }
        CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
        private void btn_return_Click(object sender, EventArgs e)
        {
            this.Hide();
            kb.Close();
        }

        private void txt_num_MouseClick(object sender, MouseEventArgs e)
        {
            CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
            kb.Location = new Point(PointToScreen(txt_num.Location).X + 65, PointToScreen(txt_num.Location).Y + 65);
            txt_num.Focus();
            txt_num.Invoke(new Action(() => { kb.Show(); }));
            Activate();
        }

        private void txt_lockId_MouseClick(object sender, MouseEventArgs e)
        {
            CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
            kb.Location = new Point(PointToScreen(txt_lockId.Location).X + 65, PointToScreen(txt_lockId.Location).Y + 65);
            txt_lockId.Focus();
            txt_lockId.Invoke(new Action(() => { kb.Show(); }));
            Activate();
        }

        private void txt_lockNum_MouseClick(object sender, MouseEventArgs e)
        {
            CabinetControl.KeyBoard kb = CabinetControl.KeyBoard.CreateInstrance();
            kb.Location = new Point(PointToScreen(txt_lockNum.Location).X + 65, PointToScreen(txt_lockNum.Location).Y + 65);
            txt_lockNum.Focus();
            txt_lockNum.Invoke(new Action(() => { kb.Show(); }));
            Activate();
        }
    }
}
